package cn.parllay.dingdangproject.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import java.util.ArrayList;
import java.util.List;

import cn.parllay.dingdangproject.R;
import cn.parllay.dingdangproject.bean.notice.Notice;
import cn.parllay.dingdangproject.utils.GlideUtils;
import cn.parllay.dingdangproject.widget.MyWindowManager1;

/**
 * Created by sunny onItemClickListener 2017/9/4.
 */

public class NoticeAdapter extends RecyclerView.Adapter<NoticeAdapter.NoticeViewHolder> implements View.OnClickListener {

    private Context context;
    private List<Notice> mNotice = new ArrayList<>();

    private OnItemClickListener onItemClickListener = null;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }

    @Override
    public void onClick(View v) {
        Notice notice = (Notice) v.getTag();
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(notice);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(Notice notice);
    }

    public NoticeAdapter(Context context) {
        this.context = context;
    }

    @Override
    public NoticeAdapter.NoticeViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(context).inflate(R.layout.item_notice_bottom, null);
        itemView.setOnClickListener(this);
        return new NoticeViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(NoticeViewHolder holder, int position) {
        String headImage = GlideUtils.checkUrl(mNotice.get(position).getHeadImage());
        GlideUtils.loadCircleImage(context, headImage, holder.notice_bottom_headImage);

        holder.itemView.setTag(mNotice.get(position));
    }

    @Override
    public int getItemCount() {
        return mNotice == null ? 0 : mNotice.size();
    }

    public void refresh(List<Notice> notices) {
        if (notices != null && notices.size() >= 0) {
            mNotice.addAll(notices);
            notifyDataSetChanged();
//            notifyItemRangeChanged(mNotice.size() - 1, notices.size());
        }
    }

    public void remove(Notice notice) {
        mNotice.remove(notice);
        notifyDataSetChanged();
        if(mNotice != null && mNotice.size() == 0) {
            MyWindowManager1.getInstance().getBottomWindow().dismiss();
        }
//        notifyItemChanged(position);
    }

    static class NoticeViewHolder extends RecyclerView.ViewHolder {

        private ImageView notice_bottom_headImage;

        public NoticeViewHolder(View itemView) {
            super(itemView);
            notice_bottom_headImage = (ImageView) itemView.findViewById(R.id.notice_bottom_headImage);
        }
    }
}
